﻿var Database = require('../app/database');
var util = require('util');
var moment = require('moment');

var Correction = module.exports = {};

log = function(msg) {
    console.log(new Date().toLocaleTimeString() + ' [corrections dates]: ' + msg);
};

getRightDate = function(value) {
    if (value != null && value.toString().length > 0) {
        var date = null;
        if (isNaN(value)) {
            date = moment(value,'DD/MM/YYYY hh:mm:ss');
            if (!date.isValid()) {
                date = moment(value,'YYYY-MM-DD hh:mm:ss');
            };
            if (!date.isValid()) {
                date = moment(value,moment.application_dateformat);
            };
        } else {
            date = moment(value);
        };
        if (date.isValid()) {
            var year = date.year();
            var month = date.month();
            var day = date.day();
            if (date.year() >= 2014 && date.month() > 1) {
                date = moment(value,'MM/DD/YYYY hh:mm:ss');
                if (!date.isValid()) {
                    date = moment(value,'YYYY-DD-MM hh:mm:ss');
                };
                if (!date.isValid()) {
                    date = null;
                };
            };
            if ( date!= null && date.year() < 2013) {
                date = null;
            };
        } else {
            date = moment(value,'DD-MM-YYYY hh:mm:ss');
        };
        if (date!= null && date.isValid()) {
            var output = date.format(moment.application_dateformat);
            log(value + ' : ' + output);
            return output;
        } else {
            log(value + ' : ');
            if (value != '1/01/0001 00:00:00') {
                log("not normal!");
            };
            return null;
        };
    };
    return null;
};

setItemRight = function(item, properties) {
    for (var j = 0; j < properties.length; j++) {
        var property = properties[j];
        item.set(property,getRightDate(item.get(property))); 
    };
};

updateUsers = function(callback) {
    new Database.Collections.Users()
    .fetch()
    .then(function(resp){
            if (resp != null && resp.length > 0){
                var count = 0;
                for (var i = 0; i < resp.length; i++) {
                    var item = resp.at(i);
                    var properties = ['blocked_datetime','last_connection'];
                    setItemRight(item, properties);
                    item.save().then(function(user){
                            count++;
                            if (count == resp.length) {
                                log('All Users are corrected: ' + resp.length);
                                if (callback) {
                                    callback();
                                };
                            };
                        });
                };
            };
        });
};

updateServers = function(callback) {
    new Database.Collections.Servers()
    .fetch()
    .then(function(resp){
            if (resp != null && resp.length > 0){
                var count = 0;
                for (var i = 0; i < resp.length; i++) {
                    var item = resp.at(i);
                    var properties = ['last_datetime'];
                    setItemRight(item, properties);
                    item.save().then(function(server){
                            count++;
                            if (count == resp.length) {
                                log('All Servers are corrected: ' + resp.length);
                                if (callback) {
                                    callback();
                                };
                            };
                        });
                };
            };
        });
};

updateActions = function(callback) {
    new Database.Collections.Actions()
    .fetch()
    .then(function(resp){
            if (resp != null && resp.length > 0){
                var count = 0;
                for (var i = 0; i < resp.length; i++) {
                    var item = resp.at(i);
                    var properties = ['action_datetime','start_datetime','end_datetime'];
                    setItemRight(item, properties);
                    item.save().then(function(action){
                            count++;
                            if (count == resp.length) {
                                log('All Actions are corrected: ' + resp.length);
                                if (callback) {
                                    callback();
                                };
                            };
                        });
                };
            };
        });
};

updatePlayers= function (callback) {
     new Database.Collections.Players()
    .fetch()
    .then(function(resp){
            if (resp != null && resp.length > 0){
                var count = 0;
                for (var i = 0; i < resp.length; i++) {
                    var item = resp.at(i);
                    var properties = ['last_datetime'];
                    setItemRight(item, properties);
                    item.save().then(function(player){
                            count++;
                            if (count == resp.length) {
                                log('All Players are corrected: ' + resp.length);
                                if (callback) {
                                    callback();
                                };
                            };
                        });
                };
            };
        });
};

updatePlayerHistories = function(callback) {
    new Database.Collections.PlayerHistories()
    .fetch()
    .then(function(resp){
            if (resp != null && resp.length > 0){
                var count = 0;
                for (var i = 0; i < resp.length; i++) {
                    var item = resp.at(i);
                    var properties = ['connect_datetime'];
                    setItemRight(item, properties);
                    item.save().then(function(history){
                            count++;
                            if (count == resp.length) {
                                log('All PlayerHistories are corrected: ' + resp.length);
                                if (callback) {
                                    callback();
                                };
                            };
                        });
                };
            };
        });
};

updateMemberRights = function(callback) {
    new Database.Collections.MemberRights()
    .fetch()
    .then(function(resp){
            if (resp != null && resp.length > 0){
                var count = 0;
                for (var i = 0; i < resp.length; i++) {
                    var item = resp.at(i);
                    var properties = ['last_datetime'];
                    setItemRight(item, properties);
                   item.save().then(function(rights){
                            count++;
                            if (count == resp.length) {
                                log('All MemberRights are corrected: ' + resp.length);
                                if (callback) {
                                    callback();
                                };
                            };
                        });
                };
            };
        });
};


updateBans= function (callback) {
     new Database.Collections.Bans()
    .fetch()
    .then(function(resp){
            if (resp != null && resp.length > 0){
                var count = 0;
                for (var i = 0; i < resp.length; i++) {
                    var item = resp.at(i);
                    var properties = ['ban_datetime'];
                    setItemRight(item, properties);
                    item.save().then(function(player){
                            count++;
                            if (count == resp.length) {
                                log('All Bans are corrected: ' + resp.length);
                                if (callback) {
                                    callback();
                                };
                            };
                        });
                };
            };
        });
};

Correction.execute = function() {
    log("Date Format : " + moment.application_dateformat);
    updateBans(function() {
        updateServers(function(){
            updateActions(function(){
                updatePlayers(function(){
                    updatePlayerHistories(function(){
                        updateMemberRights(function(){
                            updateUsers(function(){
                                log('All is Done');
                            });
                        });
                    });
                });
            });
        });   
    });
};
